<template>
  <mu-container>
    <mu-form :model="form" class="mu-demo-form" :label-position="labelPosition" label-width="100">
			<h3 class="h">{{agentName}}</h3>
      <mu-form-item prop="date" label="到站时间">
				<mu-flex align-items="center">
					<!-- <mu-date-input v-model="form.orderTime" type="dateTime" actions></mu-date-input> -->
					<mu-text-field v-model="form.orderTime" disabled ></mu-text-field>
					<mu-button flat color="primary" @click="showDatePicker">选择</mu-button>
				</mu-flex>
      </mu-form-item>
      <mu-form-item prop="select" label="燃油类型">
        <mu-select v-model="form.fuelType">
          <mu-option v-for="(index,item) in fuelType" :key="index" :label="index" :value="item"></mu-option>
        </mu-select>
      </mu-form-item>
      <mu-form-item prop="select" label="车辆年限">
        <mu-select v-model="form.useYears">
          <mu-option v-for="(index,item) in useYears" :key="index" :label="index" :value="item"></mu-option>
        </mu-select>
      </mu-form-item>
			<mu-form-item prop="select" label="车辆使用性质">
				<mu-select v-model="form.useCharacter">
					<mu-option v-for="(index,item) in useCharacter" :key="index" :label="index" :value="item"></mu-option>
				</mu-select>
			</mu-form-item>
			
			<mu-form-item prop="radio" label="服务类型">
				<mu-radio v-model="form.serverType" value="1" label="自驾车前往"></mu-radio>
				<mu-radio v-model="form.serverType" value="0" label="代理商上门取车"></mu-radio>
			</mu-form-item>
			<mu-form-item label="取车地址" prop="visitAddress" v-if="form.serverType==0">
				<mu-text-field v-model="form.visitAddress" prop="visitAddress" :max-length="30" multi-line :rows="2"></mu-text-field>
			</mu-form-item>
      <mu-button large  class="nextBtn" @click="do_order" color="primary" :disabled="isComfired" >
				<mu-icon value="check_circle" left ></mu-icon>确认</mu-button>
    </mu-form>
    <mu-dialog title="提示信息" width="360" :open.sync="openSimple">
      {{msg}}
      <mu-button slot="actions" flat color="primary" @click="closeSimpleDialog">关闭</mu-button>
    </mu-dialog>
		<mu-dialog title="确认信息" width="360" :open.sync="openSimple2">
			确认是否下单？
			<mu-button slot="actions" flat @click="closeSimpleDialog">取消</mu-button>
			<mu-button slot="actions" flat color="primary" @click="comfired">确定</mu-button>
		</mu-dialog>
		
		<div class="alert-demo-wrapper">
			<mu-alert color="warning" @delete="alert1 = false" delete v-if="alert1" transition="mu-scale-transition">
				<mu-icon left></mu-icon>{{error_msg}}
				<mu-button flat color="primary" @click="to_appForm">前往</mu-button>
			</mu-alert>
		</div>
  </mu-container>
</template>

<script>
    export default {
        name: "app_form_go",
      data () {
        return {
					alert1: false,
					isComfired: true,
					toType: 1,
          options: [
            '微型车', '小型车', '中型车', '大型车'
          ],
          msg:"",
					error_msg: '',
          fuelType: {
            1:"汽油",
            2:"柴油",
            // 3:"混合油",
            4:"液化石油气",
            5:"天然气",
//             6:"甲醇",
//             7:"乙醇",
//             8:"太阳能",
            9:"电",
//             10:"生物燃料",
//             11:"氢",
          },
          useYears:{
            0:"十年以下",
            1:"十年以上"
          },
					useCharacter: {
						0: '非营运车辆',
						1: '营运车辆'
					},
          openSimple: false,
					openSimple2: false,
          labelPosition: 'left',
          form: {
            orderTime: '',
            fuelType: '',
            useYears: '',
						useCharacter: '',
						serverType: '1',
						visitAddress: ''
          },
          select:{

          },
          agentId:"",
					agentName: ''
        }
      },
      methods:{
				
				do_order(){
					console.log(this.form)
					if(!this.form.orderTime){
						this.$toast.error("预约到站时间不能为空")
						
					}else if(!this.form.fuelType){
						this.$toast.error("燃油类型不能为空")
						
					}else if(!this.form.useYears){
						this.$toast.error("车辆年限不能为空")
						
					}else if(!this.form.useCharacter){
						this.$toast.error("车辆使用性质不能为空")
					}else if(this.form.serverType == 0 && this.isEmpty(this.form.visitAddress)){
						this.$toast.error("请填写上门取车服务的取车地址")
					}else{
						this.openSimple2=true
					}
				},
        comfired(){
					console.log(this.form)
					this.openSimple2=false
          
//           date = date.replace("T"," ");
//           date = date.slice(1,20);
					
					let date = JSON.stringify(this.form.orderTime);
          this.$ajax.post("/check-car/app/check/addOrder", {
            "agentId": this.$route.query.agentId,
            "orderTime": this.form.orderTime+":00",
            "fuelType": this.form.fuelType,
            "useYears": this.form.useYears,
						"useCharacter": this.form.useCharacter,
						"serverType": this.form.serverType,
						"visitAddress": this.form.visitAddress
          }).then((res)=> {
						console.log(res.data)
            if (res.data.code === 200) {
							this.$router.push({name:'order',query:{orderId: res.data.data.orderId}})
              // this.$router.push({name:'order',query:{orderId:res.data.orderId,orderTime:res.data.orderTime,order_subject:res.data.order_subject,basePrice:res.data.basePrice,operatePrice:res.data.operatePrice,orderMoney:res.data.orderMoney}});
            }else if (res.data.code == 500){
              this.openSimple = true;
              this.msg = res.data.msg
            }
          });
        },showDatePicker(){
					let date = new Date()
					let y = date.getFullYear()
					let m = date.getMonth() + 1
					let d = date.getDate() 
					let h = date.getHours()
					let min = date.getMinutes()
					let chooseDate = [y + '年', m + '月', d + '日', h + '时', min + '分'] 
					let minDate = [y,m,d,h,m]
					let maxDate = [y+2,m,d,h,m]
					let dataObj = {
						type: 5,
						min: minDate,
						max: maxDate,
						showChinese: true,
						value: JSON.parse(JSON.stringify(chooseDate))
					}
					this.$datepicker(dataObj).then((e) => {
						console.log(e)
						let y = e[0].replace('年','')
						let m = e[1].replace('月','')
						if(m.length == 1){
							m = '0'+m
						}
						let d = e[2].replace('日','')
						if(d.length == 1){
							d = '0'+d
						}
						let h = e[3].replace('时','')
						if(h.length == 1){
							h = '0'+h
						}
						let mm = e[4].replace('分','')
						if(mm.length == 1){
							mm = '0'+mm
						}
						this.form.orderTime = y+'-'+m+'-'+d+' '+h+':'+mm
					}).catch((e) => {
						console.log(e)
						let y = e[0].replace('年','')
						let m = e[1].replace('月','')
						if(m.length == 1){
							m = '0'+m
						}
						let d = e[2].replace('日','')
						if(d.length == 1){
							d = '0'+d
						}
						let h = e[3].replace('时','')
						if(h.length == 1){
							h = '0'+h
						}
						let mm = e[4].replace('分','')
						if(mm.length == 1){
							mm = '0'+mm
						}
						this.form.orderTime = y+'-'+m+'-'+d+' '+h+':'+mm
					})
					
				},
				isEmpty(obj){
						if(typeof obj == "undefined" || obj == null || obj == ""){
								return true;
						}else{
								return false;
						}
				},
        closeSimpleDialog () {
          this.openSimple = false;
					this.openSimple2=false
					window.location.reload()
        },to_appForm(){
					if(this.toType == 1){
						// 行驶证信息页面
						this.$router.push({name:'app_msg'})
					}else if(this.toType == 2){
						// 订单页面
						this.$router.push({name:'order_list'})
					}
					
				}
      },
			created(){
				this.agentName = this.$route.query.agentName
				this.$ajax.get("/check-car/app/check/userOrders?type=0", {})
				.then((res) => {
					if(res.data.code == 200 && res.data.data.length == 1){
						this.error_msg='您有未完成的订单'
						this.toType=2
						this.alert1=true
					}else if(res.data.code ==401){
						this.$toast.info("登陆失效，请重新登陆");
					}else{
						this.isComfired=false
					}
				})
				
// 				this.$ajax.get("/check-car/app/check/user/getCarInfo", {
// 				}).then((res)=> {
// 					console.log(res)
// 					if (res.data.code ==200){
// 						this.$ajax.get("/check-car/app/check/userOrders?type=0", {}).then((res) => {
// 							if(res.data.code == 200 && res.data.data.length == 1){
// 								this.error_msg='您有未完成的订单'
// 								this.toType=2
// 								this.alert1=true
// 							}else{
// 								this.isComfired=false
// 							}
// 						})
// 					}else if(res.data.code ==401){
// 						this.$toast.info("登陆失效，请重新登陆");
// 					}else{
// 						this.error_msg='请先上传行驶证信息'
// 						this.toType=1
// 						this.alert1=true
// 					}
// 				});
			}
    }
</script>

<style scoped>
	
	.h {
		 text-align:center
	}
	.alert-demo-wrapper {
	  width: 100%;
	  > .mu-alert {
	    margin-bottom: 16px;
	    &:last-child {
	      margin-bottom: 0;
	    }
	  }
	  > .mu-button {
	    margin: 0 auto;
	    display: block;
	  }
	}
	
	.mu-scale-transition-enter-active,
	.mu-scale-transition-leave-active {
	  transition: transform .45s cubic-bezier(0.23, 1, 0.32, 1), opacity .45s cubic-bezier(0.23, 1, 0.32, 1);
	  backface-visibility: hidden;
	}
	
	.mu-scale-transition-enter,
	.mu-scale-transition-leave-active {
	  transform: scale(0);
	  opacity: 0;
	}

</style>

